﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;

namespace YArchitech.HVAC
{
	public partial class SystemSetUpdateForm : System.Windows.Forms.Form
	{
		public SystemSetUpdateForm(Autodesk.Revit.DB.Document doc, List<LinePatternElement> linePattern, DataGridView dataGridView, List<PipingSystemType> listPipingSystemType, List<MechanicalSystemType> listMechanicaSystemType, TreeView treeView, string nowSelectedSystemCLass)
		{
			this.InitializeComponent();
			base.CenterToScreen();
			new YJKProcessTools().ShowInTaskbarChange(this);
			this.doc = doc;
			this.linePattern = linePattern;
			this.dataGridView = dataGridView;
			this.listMechanicaSystemType = listMechanicaSystemType;
			this.listPipingSystemType = listPipingSystemType;
			this.treeView = treeView;
			this.nowSelecetedSystemClass = nowSelectedSystemCLass;
			this.selectedSystemTypeName = treeView.SelectedNode.Text;
			this.textBoxColor.ReadOnly = true;
			base.Focus();
			this.loadComboxClass(this.nowSelecetedSystemClass);
		}

		public SystemSetUpdateForm(Autodesk.Revit.DB.Document doc, List<LinePatternElement> linePattern, DataGridView dataGridView, List<PipingSystemType> listPipingSystemType, List<MechanicalSystemType> listMechanicaSystemType, TreeView treeView, string nowSelectedSystemCLass, int index)
		{
			this.InitializeComponent();
			base.CenterToScreen();
			this.loadComboxClass(this.nowSelecetedSystemClass);
			this.doc = doc;
			this.linePattern = linePattern;
			this.dataGridView = dataGridView;
			this.listMechanicaSystemType = listMechanicaSystemType;
			this.listPipingSystemType = listPipingSystemType;
			this.treeView = treeView;
			this.nowSelecetedSystemClass = nowSelectedSystemCLass;
			this.selectedSystemTypeName = treeView.SelectedNode.Text;
			this.index = index;
			this.textBoxColor.ReadOnly = true;
			base.Focus();
			this.isCorrect = true;
			this.correctSystemname = SystemTypeManageForm.structList.ElementAt(index).PipeSystemType;
			this.comboBoxSystemClass.Text = SystemTypeManageForm.structList.ElementAt(index).SystemClass;
			string systemTypeCH = AssistSystemType.GetSystemTypeCH(SystemTypeManageForm.structList.ElementAt(index).PipeSystemType);
			this.comboBoxSystemType.Items.Add(systemTypeCH);
			this.comboBoxSystemType.Text = systemTypeCH;
			this.comboBoxSystemType.Enabled = false;
			this.comboBoxSystemClass.Enabled = false;
		}

		private void loadComboxClass(string sss)
		{
			this.comboBoxSystemClass.Items.Clear();
			this.comboBoxSystemClass.Items.Add("风管系统");
			this.comboBoxSystemClass.Items.Add("管道系统");
			this.comboBoxSystemClass.Text = sss;
		}

		private void SystemSetUpdateForm_Load(object sender, EventArgs e)
		{
			for (int i = 0; i < 16; i++)
			{
				this.comboBoxLineWitdh.Items.Add(i + 1);
			}
			this.comboBoxLineWitdh.Items.Add("默认");
			foreach (LinePatternElement linePatternElement in this.linePattern)
			{
				this.comboBoxLinePattern.Items.Add(linePatternElement.Name);
			}
			this.comboBoxLinePattern.Items.Add("默认");
			if (!this.isCorrect)
			{
				this.comboBoxLinePattern.Text = "默认";
				this.comboBoxLineWitdh.Text = "默认";
				this.textBoxColor.Text = "█RGB 128 128 128";
				this.textBoxColor.ForeColor = System.Drawing.Color.FromArgb(128, 128, 128);
				int num = 1;
				List<int> list = new List<int>();
				foreach (systemTypeStruct systemTypeStruct in SystemTypeManageForm.structList)
				{
					if (systemTypeStruct.PipingSystemType.Split(new char[]
					{
						' '
					}).ElementAt(0) == "新的系统")
					{
						num++;
						if (systemTypeStruct.PipingSystemType.Split(new char[]
						{
							' '
						}).Count<string>() > 1)
						{
							list.Add(int.Parse(systemTypeStruct.PipingSystemType.Split(new char[]
							{
								' '
							}).ElementAt(1).ToString()));
						}
					}
				}
				if (num != 1)
				{
					if (list.Contains(num))
					{
						num = list.Max() + 1;
					}
					this.textBoxName.Text = "新的系统" + num.ToString();
				}
				else
				{
					this.textBoxName.Text = "新的系统1";
				}
			}
			else
			{
				this.Text = "修改系统类型";
				this.textBoxName.Text = SystemTypeManageForm.structList.ElementAt(this.index).PipingSystemType;
				this.textBoxAbbreviationName.Text = SystemTypeManageForm.structList.ElementAt(this.index).PipingSystemTypeAbb;
				this.comboBoxLinePattern.Text = SystemTypeManageForm.structList.ElementAt(this.index).LinePattern;
				this.comboBoxLineWitdh.Text = SystemTypeManageForm.structList.ElementAt(this.index).LineWeight;
				this.textBoxColor.Text = SystemTypeManageForm.structList.ElementAt(this.index).Color;
				this.textBoxColor.ForeColor = System.Drawing.Color.FromArgb(int.Parse(this.textBoxColor.Text.Split(new char[]
				{
					' '
				}).ElementAt(1)), int.Parse(this.textBoxColor.Text.Split(new char[]
				{
					' '
				}).ElementAt(2)), int.Parse(this.textBoxColor.Text.Split(new char[]
				{
					' '
				}).ElementAt(3)));
			}
			base.Focus();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			ColorDialog colorDialog = new ColorDialog();
			colorDialog.AllowFullOpen = true;
			colorDialog.FullOpen = true;
			System.Drawing.Color xmldataColor = SystemManageHelper.getXMLDataColor(this.textBoxColor.Text.ToString());
			if (!xmldataColor.IsKnownColor)
			{
				int[] array = new int[16];
				array[0] = SystemManageHelper.GetIntAboutColor(xmldataColor);
				colorDialog.CustomColors = array;
			}
			colorDialog.Color = xmldataColor;
			DialogResult dialogResult = colorDialog.ShowDialog();
			if (DialogResult.OK == dialogResult)
			{
				string newRGB = SystemManageHelper.getNewRGB(colorDialog.Color.R);
				string newRGB2 = SystemManageHelper.getNewRGB(colorDialog.Color.G);
				string newRGB3 = SystemManageHelper.getNewRGB(colorDialog.Color.B);
				string text = string.Concat(new string[]
				{
					"█RGB ",
					newRGB.ToString(),
					" ",
					newRGB2.ToString(),
					" ",
					newRGB3.ToString()
				});
				this.textBoxColor.ForeColor = colorDialog.Color;
				this.textBoxColor.Text = text;
			}
		}

		private TreeNode FindNodeByCode(TreeNodeCollection nodes, string Code)
		{
			if (nodes.Count == 0)
			{
				return null;
			}
			foreach (object obj in nodes)
			{
				TreeNode treeNode = (TreeNode)obj;
				if (treeNode.Text.ToString() == Code.Trim())
				{
					return treeNode;
				}
				TreeNode treeNode2;
				if (treeNode.Nodes.Count != 0)
				{
					treeNode2 = this.FindNodeByCode(treeNode.Nodes, Code);
				}
				else
				{
					treeNode2 = null;
				}
				if (treeNode2 != null)
				{
					return treeNode2;
				}
			}
			return null;
		}

		private void buttonUpdate_Click(object sender, EventArgs e)
		{
			this.nowSelecetedSystemClass = this.comboBoxSystemClass.Text.Trim().ToString();
			AssistSystemType assistSystemType = new AssistSystemType();
			new Regex("^[\\u4e00-\\u9fa5_a-zA-Z0-9]+$");
			new Regex("^[a-zA-Z0-9_\\u4e00-\\u9fa5\\' ']+$");
			Regex regex = new Regex("\\S");
			if (this.comboBoxSystemClass.Text != "" && this.comboBoxSystemType.Text != "" && this.textBoxName.Text != "" && !assistSystemType.JudgeNameContainTeShu(this.textBoxName.Text) && this.textBoxColor.Text != "" && this.comboBoxLinePattern.Text != "" && this.comboBoxLineWitdh.Text != "" && regex.IsMatch(this.textBoxName.Text.Trim()))
			{
				bool flag = false;
				for (int i = 0; i < this.dataGridView.Rows.Count; i++)
				{
					if (!this.isCorrect)
					{
						if (this.textBoxName.Text.Trim() == this.dataGridView.Rows[i].Cells[0].Value.ToString())
						{
							flag = true;
							break;
						}
					}
					else if (this.textBoxName.Text.Trim() == this.dataGridView.Rows[i].Cells[0].Value.ToString() && i != this.index)
					{
						flag = true;
						break;
					}
				}
				if (!flag)
				{
					if (!this.isCorrect)
					{
						object[] values = new object[]
						{
							this.textBoxName.Text.Trim(),
							this.textBoxAbbreviationName.Text.Trim(),
							this.textBoxColor.Text,
							this.comboBoxLinePattern.Text,
							this.comboBoxLineWitdh.Text,
							this.comboBoxSystemClass.Text,
							AssistSystemType.GetSystemTypeUS(this.comboBoxSystemType.Text)
						};
						systemTypeStruct systemTypeStruct = new systemTypeStruct();
						systemTypeStruct.PipingSystemType = this.textBoxName.Text.Trim();
						systemTypeStruct.PipingSystemTypeAbb = this.textBoxAbbreviationName.Text.Trim();
						systemTypeStruct.PipeSystemType = AssistSystemType.GetSystemTypeUS(this.comboBoxSystemType.Text);
						systemTypeStruct.SystemClass = this.nowSelecetedSystemClass;
						if (this.textBoxColor.Text == "默认")
						{
							systemTypeStruct.Color = "█RGB 128 128 128";
						}
						else
						{
							systemTypeStruct.Color = this.textBoxColor.Text;
						}
						systemTypeStruct.LinePattern = this.comboBoxLinePattern.Text;
						systemTypeStruct.LineWeight = this.comboBoxLineWitdh.Text;
						SystemTypeManageForm.structList.Add(systemTypeStruct);
						this.dataGridView.Rows.Add(values);
					}
					else
					{
						this.dataGridView.SelectedRows[0].Cells[0].Value = this.textBoxName.Text.Trim();
						this.dataGridView.SelectedRows[0].Cells[1].Value = this.textBoxAbbreviationName.Text.Trim();
						this.dataGridView.SelectedRows[0].Cells[2].Value = this.textBoxColor.Text;
						this.dataGridView.SelectedRows[0].Cells[3].Value = this.comboBoxLinePattern.Text;
						this.dataGridView.SelectedRows[0].Cells[4].Value = this.comboBoxLineWitdh.Text;
						SystemTypeManageForm.structList.ElementAt(this.index).SystemClass = this.comboBoxSystemClass.Text;
						SystemTypeManageForm.structList.ElementAt(this.index).PipeSystemType = AssistSystemType.GetPipeSystemTypeUS(this.comboBoxSystemType.Text);
						SystemTypeManageForm.structList.ElementAt(this.index).PipingSystemType = this.textBoxName.Text.Trim();
						SystemTypeManageForm.structList.ElementAt(this.index).PipingSystemTypeAbb = this.textBoxAbbreviationName.Text.Trim();
						SystemTypeManageForm.structList.ElementAt(this.index).LinePattern = this.comboBoxLinePattern.Text;
						SystemTypeManageForm.structList.ElementAt(this.index).LineWeight = this.comboBoxLineWitdh.Text;
						SystemTypeManageForm.structList.ElementAt(this.index).Color = this.textBoxColor.Text;
					}
					base.Close();
				}
				else if (flag)
				{
					string text = this.textBoxName.Text;
					YJKMessageBox.Show("该系统类型名字在系统中已存在，请更换一个名字！");
					this.textBoxName.Text = text.Trim();
				}
				TreeNode treeNode = this.FindNodeByCode(this.treeView.Nodes, this.nowSelecetedSystemClass);
				if (treeNode != null)
				{
					treeNode.BackColor = System.Drawing.Color.DodgerBlue;
					treeNode.ForeColor = System.Drawing.Color.White;
					this.treeView.SelectedNode = treeNode;
					return;
				}
			}
			else
			{
				YJKMessageBox.Show("请确保输入均不为空,并且系统名称不能包含下列任一字符：\\:{}[]|;<>?`~！");
			}
		}

		private void buttonESC_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void textBoxColor_TextChanged(object sender, EventArgs e)
		{
			this.textBoxColor.ForeColor = System.Drawing.Color.FromArgb(int.Parse(this.textBoxColor.Text.Split(new char[]
			{
				' '
			}).ElementAt(1)), int.Parse(this.textBoxColor.Text.Split(new char[]
			{
				' '
			}).ElementAt(2)), int.Parse(this.textBoxColor.Text.Split(new char[]
			{
				' '
			}).ElementAt(3)));
		}

		private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (!this.isCorrect)
			{
				if (this.comboBoxSystemClass.Text == "风管系统")
				{
					this.comboBoxSystemType.Items.Clear();
					this.comboBoxSystemType.Items.Add("送风");
					this.comboBoxSystemType.Items.Add("回风");
					this.comboBoxSystemType.Items.Add("排风");
					if (this.selectedSystemTypeName != "" && this.comboBoxSystemType.Items.Contains(this.selectedSystemTypeName))
					{
						this.comboBoxSystemType.Text = this.selectedSystemTypeName;
					}
					else
					{
						this.comboBoxSystemType.Text = "送风";
					}
				}
				if (this.comboBoxSystemClass.Text == "管道系统")
				{
					this.comboBoxSystemType.Items.Clear();
					this.comboBoxSystemType.Items.Add("循环供水");
					this.comboBoxSystemType.Items.Add("循环回水");
					this.comboBoxSystemType.Items.Add("通风孔");
					this.comboBoxSystemType.Items.Add("干式消防系统");
					this.comboBoxSystemType.Items.Add("湿式消防系统");
					this.comboBoxSystemType.Items.Add("预作用消防系统");
					this.comboBoxSystemType.Items.Add("其他消防系统");
					this.comboBoxSystemType.Items.Add("卫生设备");
					this.comboBoxSystemType.Items.Add("家用热水");
					this.comboBoxSystemType.Items.Add("家用冷水");
					this.comboBoxSystemType.Items.Add("其他");
					if (this.selectedSystemTypeName != "" && this.comboBoxSystemType.Items.Contains(this.selectedSystemTypeName))
					{
						this.comboBoxSystemType.Text = this.selectedSystemTypeName;
						return;
					}
					this.comboBoxSystemType.Text = "循环供水";
					return;
				}
			}
			else
			{
				string systemTypeCH = AssistSystemType.GetSystemTypeCH(SystemTypeManageForm.structList.ElementAt(this.index).PipeSystemType);
				this.comboBoxSystemType.Items.Add(systemTypeCH);
				this.comboBoxSystemType.Text = systemTypeCH;
				this.comboBoxSystemType.Enabled = false;
			}
		}

		private Document doc;

		private List<LinePatternElement> linePattern = new List<LinePatternElement>();

		private List<PipingSystemType> listPipingSystemType = new List<PipingSystemType>();

		private List<MechanicalSystemType> listMechanicaSystemType = new List<MechanicalSystemType>();

		private DataGridView dataGridView;

		private TreeView treeView;

		private string nowSelecetedSystemClass = "";

		private int index;

		private bool isCorrect;

		private string correctSystemname = "";

		private string selectedSystemTypeName = "";
	}
}
